﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.SessionState;
using PreguntaAlExperto.RulesLayer.Services;
using PreguntaAlExperto.RulesLayer.Entities;
using System.Net.Mail;

namespace PreguntaAlExperto
{
    public class ProcessVote
        : IHttpHandler, IRequiresSessionState
    {

        public bool IsReusable
        {
            get { return true; }
        }

        public void ProcessRequest(HttpContext context)
        {
            try
            {
                if (!string.IsNullOrEmpty(context.Request.QueryString["Points"]))
                {
                    if (int.Parse(context.Request.QueryString["Points"].ToString()) >= 1 &&
                        int.Parse(context.Request.QueryString["Points"].ToString()) <= 5)
                    {
                        ArticlesService _service = new ArticlesService();
                        _service.updateVote(context.Request.QueryString["URL"].ToString(), int.Parse(context.Request.QueryString["Points"].ToString()));
                        Article _article = _service.getArticleByPath(context.Request.QueryString["URL"]);
                        context.Response.Write((_article.TotalPoints / _article.TotalVotes).ToString() + "," + _article.TotalVotes.ToString());
                    }
                }
            }
            catch
            {
                context.Response.Write("1,0");
            }
            finally
            { }
        }


    }
}
